package com.ysm.smfu.utils;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.widget.PopupWindow;

import com.ysm.smfu.R;

import java.util.Hashtable;

/**
 * Created by Administrator on 2016/9/23.
 */
public class PopupWindowUtil {

    private static Hashtable<String, PopupWindow> hashtable = new Hashtable<>();
    private static int width;
    private static PopupWindow pw;

    /**
     * 切换popuWindow显示/隐藏
     */
    public static View togglePopup(String className, int id, View view) {
        //TODO 写死了大小
        return togglePopup(className, id, view, 460, 320, 35, 30);
    }

    /**
     * 切换popuWindow显示/隐藏,根据传入值设置大小
     *
     * @param className 不同类的标记
     * @param id        需要显示的布局id
     * @param view      显示在view之下
     * @param width     显示宽度
     * @param height    显示高度
     * @param x         x方向偏移量
     * @param y         y方向偏移量
     */
    public static View togglePopup(String className, int id, View view, int width, int height, int x, int y) {
        PopupWindow mPopupWindow = hashtable.get(className);

        if (mPopupWindow == null) {
            View contentView = UIUtils.inflate(id);
            mPopupWindow = new PopupWindow(contentView, width, height, true);
            mPopupWindow.setTouchable(true);
            mPopupWindow.setOutsideTouchable(true);
            mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            hashtable.put(className, mPopupWindow);
        }
        if (mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
//                mPopupWindow = null;
        } else {
            mPopupWindow.showAsDropDown(view, x, y);
        }
        return mPopupWindow.getContentView();
    }

    /**
     * 显示在控件下方
     *
     * @param className
     * @param id
     * @param view
     * @param y
     * @return
     */
    public static View togglePopup(String className, int id, View view, int y) {
        PopupWindow mPopupWindow = hashtable.get(className);

        if (mPopupWindow == null) {
            View contentView = UIUtils.inflate(id);
            contentView.measure(0, 0);
            width = contentView.getMeasuredWidth();
            int height = contentView.getMeasuredHeight();
            mPopupWindow = new PopupWindow(contentView, width, height, true);
            mPopupWindow.setTouchable(true);
            mPopupWindow.setOutsideTouchable(true);
            mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            hashtable.put(className, mPopupWindow);
        }
        if (mPopupWindow != null && mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
//                mPopupWindow = null;
        } else {
            if (!mPopupWindow.isShowing())
                mPopupWindow.showAsDropDown(view, -width * 3 / 4, y);
        }
        return mPopupWindow.getContentView();
    }

    /**
     * 从底部弹出
     *
     * @param activity
     * @param className
     * @param view
     */
    public static View toogleInButton(Activity activity, final String className, View view, int id, final View shade) {
        pw = hashtable.get(className);
        if (pw == null) {
            View contentView = UIUtils.inflate(id);
            contentView.measure(0, 0);
            int height = contentView.getMeasuredHeight();
            pw = new PopupWindow(contentView, activity.getWindowManager().getDefaultDisplay().getWidth(), height, true);
            pw.setAnimationStyle(R.style.pw);
            pw.setOutsideTouchable(true);
            pw.setTouchable(true);
            hashtable.put(className, pw);
        }

        if (pw != null && pw.isShowing()) {
            pw.dismiss();
//                shade.setVisibility(View.GONE);
        } else {
//            // 添加遮罩
//            shade.setVisibility(View.VISIBLE);
            if (pw != null &&!pw.isShowing())
                pw.showAtLocation(view, Gravity.BOTTOM, 0, 0);
        }
//        pw.setOnDismissListener(new PopupWindow.OnDismissListener() {
//            @Override
//            public void onDismiss() {
//                if (shade.getVisibility() == View.VISIBLE)
//                    cancel(className, shade);
//            }
//        });
        return pw.getContentView();
    }

    public static void cancel(String className, View shade) {
        PopupWindow popupWindow = hashtable.get(className);
        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
            if (shade != null)
                shade.setVisibility(View.GONE);

        }
    }
}
